local zhenglue = fk.CreateSkill {
  name = "zhenglue",
}

zhenglue:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return target.role == "lord"
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = self.name, prompt = "#zhenglue1-trigger" })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, self.name)
    if player.dead then return end
    local targets = table.filter(room.alive_players, function(p)
      return p:getMark("@@caocao_lie") == 0
    end)
    if #targets == 0 then return end
    local x = 1
    if #targets > 1 and #player.room.logic:getActualDamageEvents(1, function(e)
          local damage = e.data
          if target == damage.from then
            return true
          else
            return false
          end
        end, Player.HistoryTurn) == 0 then
      x = 2
    end
    local tos = room:askToChoosePlayers(player,
      {
        targets = targets,
        min_num = 1,
        max_num = x,
        prompt = "#zhenglue-choose:::" .. tostring(x),
        skill_name = self.name,
        cancelable = false
      })
    for _, to in ipairs(tos) do
      room:setPlayerMark(to, "@@caocao_lie", 1)
    end
  end,
})

zhenglue:addEffect(fk.Damage, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return target and target == player and data.to:getMark("@@caocao_lie") > 0 and
          player:getMark("zhenglue-turn") == 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = self.name, prompt = "#zhenglue2-trigger" })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, self.name)
    if player.dead then return end
    room:setPlayerMark(player, "zhenglue-turn", 1)
    if data.card and room:getCardArea(data.card) == Card.Processing then
      room:moveCardTo(data.card, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player)
    end
  end,
})

zhenglue:addEffect('targetmod',{
  bypass_times = function(self, player, skill, scope, card, to)
    return card and player:hasSkill("zhenglue") and to and to:getMark("@@caocao_lie") > 0
  end,
  bypass_distances = function(self, player, skill, card, to)
    return card and player:hasSkill("zhenglue") and to and to:getMark("@@caocao_lie") > 0
  end,
})

return zhenglue
